<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>公司管理</el-breadcrumb-item>
      <el-breadcrumb-item>所有公司</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="goAddpage">添加公司</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table @expand-change="expandSelect" ref="refTable" :data="companyList" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="公司名称：">
                <span>{{ scope.row.name }}</span>
              </el-form-item>
              <el-form-item label="公司 ID：">
                <span>{{ scope.row.id }}</span>
              </el-form-item>
              <el-form-item label="所在城市：">
                <span>{{ scope.row.city }}</span>
              </el-form-item>
              <el-form-item label="公司地址：">
                <span>{{ scope.row.site }}</span>
              </el-form-item>
              <el-form-item class="brief" label="公司简介：">
                <span>{{ scope.row.brief }}</span>
              </el-form-item>
            </el-form>
            <el-image style="width: 100px; height: 100px" :src="scope.row.url" fit="fill"></el-image>
            <div class="el-uploadx">
              <el-upload
                action="http://gz.pwbov.cn/upload"
                :on-remove="handleRemove"
                :file-list="scope.row.urllist"
                list-type="picture"
                :on-success="(response, file, fileList) => uploadSucSingle(response, file, fileList,scope.row)"
                :limit="1"
              >
                <el-button size="small" type="primary">更换Logo</el-button>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过5MB</div>
              </el-upload>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="公司名称" prop="name"></el-table-column>
        <el-table-column label="公司地址" prop="site"></el-table-column>
        <el-table-column label="所在城市" prop="city"></el-table-column>
        <el-table-column label="热度" width="155px">
          <template slot-scope="scope">
            <el-input-number
              v-model="scope.row.heat"
              @change="changeHeat(scope.row.id,scope.row.heat)"
              :min="0"
              :max="100"
              size="small"
              :disabled="$store.getters.iftype3"
              :controls="false"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showBox(scope.row)">修改</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delCompanyById(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 修改公司信息 -->
    <el-dialog title="修改公司信息" :visible.sync="showPutBox" width="30%" @close="putClose">
      <el-form :model="putFrom" :rules="putRules" ref="putRef" label-width="80px">
        <el-form-item label="公司名称" prop="name">
          <el-input v-model="putFrom.name"></el-input>
        </el-form-item>
        <el-form-item label="公司地址" prop="site">
          <el-input v-model="putFrom.site"></el-input>
        </el-form-item>
        <el-form-item label="所在城市" prop="city">
          <el-select v-model="putFrom.city" placeholder="请选择城市">
            <el-option v-for="(item,i) in city" :key="i" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司简介" prop="brief">
          <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 6}" v-model="putFrom.brief"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showPutBox = false">取 消</el-button>
        <el-button type="primary" @click="putData">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      total: 0,
      pageNum: 1,
      userList: [],
      companyList: [],
      showPutBox: false,
      putFrom: {},
      putRules: {
        name: [{ required: true, message: '请输入公司名称', trigger: 'blur' }],
        site: [{ required: true, message: '请输入活动地址', trigger: 'blur' }],
        city: [{ required: true, message: '请选择所在城市', trigger: 'blur' }],
        brief: [{ required: true, message: '请输入活动简介', trigger: 'blur' }]
      },
      city: [
        '全国',
        '北京市',
        '上海市',
        '天津市',
        '重庆市',
        '河北省',
        '山西省',
        '辽宁省',
        '吉林省',
        '黑龙江省',
        '江苏省',
        '浙江省',
        '安徽省',
        '福建省',
        '江西省',
        '山东省',
        '河南省',
        '湖北省',
        '湖南省',
        '广东省',
        '海南省',
        '四川省',
        '贵州省',
        '云南省',
        '陕西省',
        '甘肃省',
        '青海省',
        '台湾省'
      ]
    }
  },
  created() {
    this.getCompanyList()
  },
  methods: {
    async getCompanyList() {
      const { data: res } = await this.$http.get('company/all', {
        params: {
          pageSize: 10,
          pageNum: this.pageNum
        }
      })
      if (res.meta.status != 200) {
        return this.$message.error('获取公司列表失败！')
      }
      this.companyList = res.data.list
      this.total = res.data.total
    },
    handleCurrentChange(newPage) {
      this.pageNum = newPage
      this.getUserList()
    },
    goAddpage() {
      this.$router.push('/company/add')
    },
    // 根据 id 删除公司
    delCompanyById(id) {
      this.$confirm('此操作将永久删除该公司, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`company`, {
            data: { id: id }
          })
          if (res.meta.status != 200) {
            return this.$message.error('删除公司失败！')
          }
          this.$message.success('删除公司成功！')
          this.getCompanyList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 修改热度
    async changeHeat(id, heat) {
      const { data: res } = await this.$http.put('company', { id: id, heat: heat })
      if (res.meta.status != 200) {
        return this.$message.error('修改热度失败！')
      }
      this.$message.success('修改热度成功！')
      this.getCompanyList()
    },
    // 展示修改 Box
    showBox(data) {
      this.showPutBox = true
      this.putFrom = data
    },
    // 关闭修改 Box
    putClose() {
      this.showPutBox = false
      this.$refs.putRef.resetFields()
    },
    // 修改公司信息
    putData() {
      this.$refs.putRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('company', this.putFrom)
        if (res.meta.status != 200) {
          return this.$message.error('修改信息失败！')
        }
        this.$message.success('修改信息成功！')
        this.showPutBox = false
        this.getCompanyList()
      })
    },
    // 控制手风琴效果
    expandSelect(row, expandedRows) {
      if (expandedRows.length > 1) {
        this.expands = []
        if (row) {
          this.expands.push(row)
        }
        this.$refs.refTable.toggleRowExpansion(expandedRows[0])
      } else {
        this.expands = []
      }
    },
    // 上传logo
    async uploadSucSingle(response, file, list, datae) {
      console.log(datae)
      const { data: ress } = await this.$http.put('company', { id: datae.id, url: response.data.url })
      if (ress.meta.status != 200) {
        return this.$message.error('修改logo失败！')
      }
      this.$message.success('修改logo成功！')
      console.log(datae)
      datae.url = response.data.url
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    }
  }
}
</script>

<style lang="less" scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 100%;
}
.el-image {
  float: left;
}
.el-uploadx {
  margin-top: 30px;
  margin-left: 30px;
  float: left;
}
</style>